/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.applications;

import elaborate.tag_analysis.client.MainFrame;
import elaborate.tag_analysis.client.utils.resource_logs.ResourceLogHandler;
import elaborate.tag_analysis.client.utils.resource_logs.ResourceLogs;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lendle
 */
public abstract class AbstractApplication implements Application{
    protected ApplicationContext context = null;
    protected ResourceLogs resourceLogs = null;
    protected DaoFactory daoFactory=null;
    
    protected File getApplicationFolder(){
        File folder=new File("applications"+File.separator+this.getApplicationId());
        if(folder.exists()==false || !folder.isDirectory()){
            folder.mkdirs();
        }
        return folder;
    }
    
    @Override
    public void init(ApplicationContext context) {
        File folder=this.getApplicationFolder();
        this.context = context;
        resourceLogs = this.loadResourceLogs();
        daoFactory=new DefaultDaoFactory();
    }
    
    protected ResourceLogs loadResourceLogs() {
        File file = new File("applications"+File.separator+this.getApplicationId()+File.separator+"resource-logs.xml");
        if (file.exists()) {
            ResourceLogHandler handler = new ResourceLogHandler();
            InputStreamReader reader = null;
            try {
                reader = new InputStreamReader(new FileInputStream(file), "utf-8");
                return handler.load(reader);
            } catch (Exception ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            } finally {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            return new ResourceLogs();
        }
    }

    @Override
    public void closing() {
        OutputStreamWriter writer=null;
        try {
            writer=new OutputStreamWriter(new FileOutputStream("applications"+File.separator+this.getApplicationId()+File.separator+"resource-logs.xml"), "utf-8");
            new ResourceLogHandler().save(writer, resourceLogs);
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex);
        }finally{
            try {
                writer.close();
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public DaoFactory getDaoFactory() {
        return this.daoFactory;
    }
    
    
}
